﻿/// <reference path="wftree.ts" />
/// <reference path="../../repository/soc.ts" />

 module WorkBasket {
    export module design {


         var vm = {


            SelectedProcessVersion: ko.observable(new WorkBasket.Entities.ProcessVersion(
                ko.observable(0),
                ko.observable(Date.now()),
                ko.observable("1.0"),
                ko.observable(0))
                ),
            FieldTypes: ko.observableArray(["Text", "Number", "Date", "Calculation"]),
            NewWorkflow: ko.observable(),
            NewProcessVersion: ko.observable(),
            NewField: ko.observable(),
            Workflows: ko.observableArray([]),
            SelectedWorkflow: ko.observable(new WorkBasket.Entities.Workflow(
                ko.observable(0),
                ko.observable("")
                )),

            loadworkflows: function () {

                WorkBasket.PleaseWait(true);

                var SOC = WorkBasket.SOC;
                var arr = SOC.Workflows().GetAll(null);
                arr.forEach(function (o: any) {

                    vm.Workflows().push(o);
                });

                WorkBasket.PleaseWait(false);

            },


            UpdateWorkflow: function (data: any, event: any) {
                var SOC = WorkBasket.SOC;


                if (event !== undefined) {
                    if (data.SelectedWorkflow().WorkflowName().length > 0) {

                        SOC.Workflows().SaveData(data.SelectedWorkflow());


                    }

                }
            },
            AddWorkflow: function (data, event) {

                var SOC = WorkBasket.SOC;
                if (event !== undefined) {
                    //Load a wf with observables to the viewmodel
                    var wf: WorkBasket.Entities.Workflow = new WorkBasket.Entities.Workflow
                        (
                        ko.observable(0),
                        ko.observable("My New Workflow")
                        );


                    if (wf.WorkflowName().length > 0) {
                        var obj:any = SOC.Workflows().SaveData(wf);
                        wf.WorkflowID(obj.WorkflowID);
                        this.Workflows.push(wf);

                    }
                }
            },
            DeleteWorkflow: function (data: any) {
                var SOC = WorkBasket.SOC;

                SOC.Workflows().Delete(data.SelectedWorkflow(), data.SelectedWorkflow().WorkflowID());

                this.Workflows.remove(data.SelectedWorkflow());



            },
            AddField: function () {

                var SOC = WorkBasket.SOC;
                var fld: WorkBasket.Entities.Field = new WorkBasket.Entities.Field(
                    ko.observable(0),
                    ko.observable(this.NewField()),
                    ko.observable(this.SelectedWorkflow().WorkflowID()),
                    ko.observable('Text'),
                    ko.observable(''));
                var obj: any = SOC.Fields().SaveData(fld, fld.FieldID());
                fld.FieldID(obj.FieldID());
                this.SelectedWorkflow().Fields.push(fld);
                this.NewField("");


            },
            DeleteField: function (data) {
                var SOC = WorkBasket.SOC;

                SOC.Fields().Delete(data, data.FieldID);
                this.SelectedWorkflow().Fields.remove(data);

            },
            UpdateField: function (data) {
                var SOC = WorkBasket.SOC;

                SOC.Fields().SaveData(data, data.FieldID());
                 


            },
            AddRootNode: function () {
                Tree.addRoot(this.SelectedProcessVersion().ProcessVersionID());
            },
            AddProcessVersion: function () {
                var pv = new WorkBasket.Entities.ProcessVersion(
                    ko.observable(0),
                    ko.observable(Date.now),
                    this.NewProcessVersion(),
                    this.SelectedWorkflow().WorkflowID()
                    );
                
                
                var obj = SOC.ProcessVersions().SaveData(pv, null);
                this.SelectedWorkflow().ProcessVersions.push(obj);
                this.NewProcessVersion("");
                $('.datepicker').datepicker({ dateFormat: 'yy-dd-mm' });
            },

            UpdateProcessVersion: function (data: any, event: any) {
                var obj: any;
                obj = SOC.ProcessVersions().SaveData(data);
       

            },

             SelectProcessVersion: function (obj) {
                // alert('SelectProcessVersion');  //this will populate the tree from DB
                 WorkBasket.Tree.SetProcessVersionID(obj.ProcessVersionID());

              
                $("#jstree").jstree().refresh(false);
              
            },
            DeleteVersion: function (obj) {
                SOC.ProcessVersions().Delete(obj, obj.ProcessVersionID);
                this.SelectedWorkflow().ProcessVersions.remove(obj);
            }

        };


   
        vm.loadworkflows();
    
        ko.bindingHandlers["WFChange"] = {
            init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
                // This will be called when the binding is first applied to an element
                // Set up any initial state, event handlers, etc. here
            },
            update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
             
                var SOC = WorkBasket.SOC;
                var wfid = ko.unwrap(valueAccessor()).WorkflowID()
                var pv:any = SOC.ProcessVersions().GetByParentKey(wfid);
                if (pv == null) {
                    pv = ko.observable(new WorkBasket.Entities.ProcessVersion(
                        ko.observable(0),
                        ko.observable(Date.now()),
                        "1.0",
                        wfid)
                        );
                
                }

               // vm.SelectedWorkflow().ProcessVersions=pv;
                
            },


        };
      
        ko.applyBindings(vm);
 
         
        $('.datepicker').datepicker({ dateFormat: 'yy-dd-mm' });


 

   
    }

    

}